package com.atguigu.crowd.funding.handler;

import com.atguigu.crowd.funding.entity.ResultEntity;
import com.atguigu.crowd.funding.entity.Role;
import com.atguigu.crowd.funding.mapper.RoleMapper;
import com.atguigu.crowd.funding.service.api.RoleService;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @author hftang
 * @date 2019-10-28 10:02
 * @desc
 */
@RestController
public class RoleHandler {

    @Autowired
    private RoleService roleService;

    @RequestMapping("/role/search/by/keyword")
    public ResultEntity<PageInfo<Role>> search(@RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                               @RequestParam(value = "pageSize", defaultValue = "5") Integer pageSize,
                                               @RequestParam("keyword") String keyword) {

        PageInfo<Role> rolePageInfo = this.roleService.queryForKeywordWithPage(pageNum, pageSize, keyword);


        return ResultEntity.successWithData(rolePageInfo);

    }

    @RequestMapping("role/get/list/by/id/list")
    public ResultEntity<List<Role>> queryRoleByIds(@RequestBody List<Integer> roleIdList) {
        System.out.println("收到的roleid" + roleIdList);
        List<Role> roleList = this.roleService.queryRoleByIds(roleIdList);
        return ResultEntity.successWithData(roleList);

    }

    @RequestMapping("/role/batch/remove.json")
    public ResultEntity batchRemove(@RequestBody List<Integer> roleIds) {
        this.roleService.batchRemove(roleIds);
        return ResultEntity.successWithoutData();
    }

    //添加
    @RequestMapping("/role/save/role")
    public ResultEntity<String> saveRole(@RequestParam("roleName") String roleName) {
        this.roleService.saveRole(roleName);
        return ResultEntity.successWithoutData();
    }

//    @RequestMapping("/role/update/role")
//    public ResultEntity<String> updateRole(@RequestParam("roleId") String id, @RequestParam("roleName") String roleName) {
//        Role role = new Role(Integer.parseInt(id), roleName);
//        this.roleService.updateRole(role);
//
//        return ResultEntity.successWithoutData();
//    }

    @RequestMapping("/role/update/role")
    public ResultEntity<String> updateRole(Role role) {
        this.roleService.updateRole(role);

        return ResultEntity.successWithoutData();
    }

}
